package com.example.hcd.internet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

public class LinkHelp {

	private String cookies;
    public static HttpClient sHttpClient;
    static {
        BasicHttpParams params = new BasicHttpParams();

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
        schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
        // Set the timeout in milliseconds until a connection is established.
        HttpConnectionParams.setConnectionTimeout(params, 15000);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setSoTimeout(params, 15000);
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        sHttpClient = new DefaultHttpClient(cm, params);
        
        
    }
    
//    public static HttpResponse login(String uri, List<NameValuePair> p, boolean need) throws Exception {
//        HttpPost request = new HttpPost(uri);
//
//        
//        if (p != null) {
//            request.setEntity(new UrlEncodedFormEntity(p, HTTP.UTF_8));
//        }
//        HttpParams params = new BasicHttpParams();
//        if (need) {
//            HttpConnectionParams.setConnectionTimeout(params, 15000);
//        } else {
//            HttpConnectionParams.setConnectionTimeout(params, 3000);
//        }
//        request.setParams(params);
//        HttpResponse response = null;
//        sHttpClient.getConnectionManager().closeExpiredConnections();
//        response = sHttpClient.execute(request);
//        
//        Header header = response.getFirstHeader("Set-Cookie");
//        if (header != null) {
//            InternetKeep.getInstence().cookie = header.getValue();
//            String sd = InternetKeep.getInstence().cookie;
//        }
//
//        return response;
//    }
    
//    public static HttpResponse post(String uri, List<NameValuePair> p, boolean need) throws Exception {
//    	HttpClient sHttpClient= new DefaultHttpClient();
//    	HttpPost request = new HttpPost(uri);
//        request.addHeader("Cookie", InternetKeep.getInstence().cookie);
//        if (p != null) {
//            request.setEntity(new UrlEncodedFormEntity(p, HTTP.UTF_8));
//        }
//        HttpParams params = new BasicHttpParams();
//        if (need) {
//            HttpConnectionParams.setConnectionTimeout(params, 15000);
//        } else {
//            HttpConnectionParams.setConnectionTimeout(params, 3000);
//        }
//        request.setParams(params);
//        HttpResponse response = null;
//        sHttpClient.getConnectionManager().closeExpiredConnections();
//        response = sHttpClient.execute(request);
//        return response;
//    }
    
    

    public static HttpResponse post(String uri, List<NameValuePair> p, boolean need) throws Exception {
        HttpPost request = new HttpPost(uri);
        //request.addHeader("Cookie", InternetKeep.getInstence().cookie);
        if (p != null) {
            request.setEntity(new UrlEncodedFormEntity(p, HTTP.UTF_8));
        }
        HttpParams params = new BasicHttpParams();
        if (need) {
            HttpConnectionParams.setConnectionTimeout(params, 15000);
        } else {
            HttpConnectionParams.setConnectionTimeout(params, 3000);
        }
        request.setParams(params);
        HttpResponse response = null;
        sHttpClient.getConnectionManager().closeExpiredConnections();
        response = sHttpClient.execute(request);
        return response;
    }
    
    public static HttpResponse UpPhotos(String uri, MultipartEntity multipartEntity,boolean need) throws Exception {
        HttpPost request = new HttpPost(uri);
        //request.addHeader("Cookie", InternetKeep.getInstence().cookie);
        request.setEntity(multipartEntity);
        
        
        HttpParams params = new BasicHttpParams();
        if (need) {
            HttpConnectionParams.setConnectionTimeout(params, 15000);
        } else {
            HttpConnectionParams.setConnectionTimeout(params, 3000);
        }
        request.setParams(params);
        HttpResponse response = null;
        sHttpClient.getConnectionManager().closeExpiredConnections();
        response = sHttpClient.execute(request);
        return response;
    }

    /*
     * ����ȡ����Httpclient;
     */
    public static HttpResponse post(String uri, List<NameValuePair> p, HttpClient h) throws Exception {
        HttpPost request = new HttpPost(uri);
        //request.addHeader("Cookie", InternetKeep.getInstence().cookie);
        if (p != null) {
            request.setEntity(new UrlEncodedFormEntity(p, HTTP.UTF_8));
        }
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 15000);
        request.setParams(params);
        HttpResponse response = null;
        response = h.execute(request);
        return response;
    }
    
    
    public static HttpResponse uploadPhoto(String uri,Bitmap bitmap,boolean need) throws Exception
    {
    	HttpPost httppost = new HttpPost(uri);
    	//httppost.addHeader("Cookie", InternetKeep.getInstence().cookie);
        HttpParams params = new BasicHttpParams();
        if (need) {
            HttpConnectionParams.setConnectionTimeout(params, 15000);
        } else {
            HttpConnectionParams.setConnectionTimeout(params, 3000);
        }
        httppost.setParams(params);
        HttpResponse response = null;
        MultipartEntity multipartEntity = new MultipartEntity();
    	ByteArrayOutputStream photoBao = new ByteArrayOutputStream();
    	boolean successCompress = bitmap.compress(CompressFormat.JPEG,  
                80, photoBao);
    	
    	ByteArrayBody byteArrayBody = new ByteArrayBody(  
                photoBao.toByteArray(), "userPhoto.jpg");  
    	
    	multipartEntity.addPart("jpg", byteArrayBody);
    	httppost.setEntity(multipartEntity);
    	sHttpClient.getConnectionManager().closeExpiredConnections();
        response = sHttpClient.execute(httppost);  
        
        
        
        return response;
    	
    }
    
    public static HttpResponse uploadFiles(String uri,ArrayList<String> fileUris,boolean need) throws Exception
    {
    	HttpPost httppost = new HttpPost(uri);
    	//httppost.addHeader("Cookie", InternetKeep.getInstence().cookie);
        HttpParams params = new BasicHttpParams();
        if (need) {
            HttpConnectionParams.setConnectionTimeout(params, 15000);
        } else {
            HttpConnectionParams.setConnectionTimeout(params, 3000);
        }
        httppost.setParams(params);
        HttpResponse response = null;
        MultipartEntity multipartEntity = new MultipartEntity();
    	int count=0;
        for(String fileuri:fileUris)
        {
        	File file = new File(fileuri);
        	FileBody filebody = new FileBody(file);
        	multipartEntity.addPart("file"+(count++), filebody);
        }

    	httppost.setEntity(multipartEntity);
    	sHttpClient.getConnectionManager().closeExpiredConnections();
        response = sHttpClient.execute(httppost);  
        
        
        
        return response;
    	
    }

    public static HttpResponse get(String uri, boolean need) throws Exception {
        HttpGet get = new HttpGet(uri);
        get.addHeader("Cookie", InternetKeep.getInstence().cookie);
        HttpParams params = new BasicHttpParams();
        if (need) {
            HttpConnectionParams.setConnectionTimeout(params, 15000);
        } else {
            HttpConnectionParams.setConnectionTimeout(params, 2000);
        }
        get.setParams(params);
        HttpResponse response = null;
        response = sHttpClient.execute(get);
        return response;
    }

    public static void shutdownHttpClient() {
        if (sHttpClient != null && sHttpClient.getConnectionManager() != null) {
            sHttpClient.getConnectionManager().shutdown();
        }
    }
}
